package com.daka.service.impl;

import com.daka.dao.ActivitySignDao;
import com.daka.entity.Activity;
import com.daka.entity.ActivitySign;
import com.daka.entity.Member;
import com.daka.entity.Organization;
import com.daka.service.ActivityService;
import com.daka.service.ActivitySignService;
import com.daka.service.MemberService;
import com.daka.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/9/18 16:40
 */

@Service("activitySignService")
@Transactional
public class ActivitySignServiceImpl implements ActivitySignService {

    @Autowired
    private ActivitySignDao activitySignDao;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private OrganizationService organizationService;

    @Override
    public int insert(ActivitySign po) {
        return activitySignDao.insert(po);
    }

    @Override
    public int update(ActivitySign po) {
        return activitySignDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public ActivitySign selectByPrimaryKey(Integer id) {
        return activitySignDao.selectByPrimaryKey(id);
    }

    @Override
    public List<ActivitySign> selectByStatement(ActivitySign po) {
        return activitySignDao.selectByStatement(po);
    }

    @Override
    public int selectByStatementCount(ActivitySign po) {
        return activitySignDao.selectByStatementCount(po);
    }

    @Override
    public ActivitySign findByActivityAndVolunteer(Integer activityId,Integer member ) {
        return activitySignDao.findByActivityAndVolunteer(activityId, member);
    }

    /**
     * 更新所有地方的志愿者时间信息
     * @param activitySign 打卡信息
     * @param oldTime 上次记录时间
     * @return 打卡结果
     */
    @Override
    public ActivitySign updateTimes(ActivitySign activitySign, int oldTime) {

        //保存公益记录
        this.activitySignDao.updateByPrimaryKeySelective(activitySign);

        if(activitySign.getVolunteerTime()>0) {
            //更新活动的累计时间
            Activity activity = activityService.selectByPrimaryKey(activitySign.getActivityId());
            activity.setTimes(activity.getTimes() + activitySign.getVolunteerTime() - oldTime);
            activityService.update(activity);

            //更新当前志愿者的累计时间
            Member member = memberService.selectByPrimaryKey(activitySign.getMember());
            member.setVolunteerTime(member.getVolunteerTime() + activitySign.getVolunteerTime() - oldTime);
            memberService.update(member);

            //更新社团的累计时间
            Organization organization = organizationService.selectByPrimaryKey(activity.getOrganization());
            organization.setTimes(organization.getTimes()+activitySign.getVolunteerTime()-oldTime);
            organization.setAllTimes(organization.getTimes() + activitySign.getVolunteerTime() - oldTime);
            organizationService.update(organization);

            //更新当前会员在当前社团的累计完成的公益时间
//            MemberOrg memberOrg = new MemberOrg();
//            memberOrg.setMember(member.getId());
//            memberOrg.setOrgid(organization.getId());
//            List<MemberOrg> list = memberOrgService.selectByStatement(memberOrg);
//            if(list!= null && list.size()>0){
//                memberOrg = list.get(0);
//            }
//            memberOrg.setTimes(memberOrg.getTimes()+activitySign.getTimes()-oldTime);
//            this.groupVolunteerService.update(memberOrg);
//            if(activitySign.getTimes()>0) {
//                //如果签到次数>2时，则不在进行提示
//                PushActivityMesssage.getInstance().activityDiary(activity.getName(), volunteer.getMemberCode(), activity.getActivityCode(), organization.getGroupCode());
//                Volunteer monitorVolunteer = volunteerService.findOne(activity.getMonitor());
//                PushActivityMesssage.getInstance().commentVolunteerList(volunteer.getRealName(), monitorVolunteer.getMemberCode(), activity.getActivityCode(),activity.getName());
//            }
        }

        return activitySign;
    }

}
